#include <generik/tracing.h>
GENERIK_TRACER_GLOBAL;

#include <generik/threading.h>

#include "values.h"
#include "options.h"

#include <list>
#include <fstream>

struct processor
: generik::threadgenerator<std::string, values>
{
	std::list<std::string> _input;
	std::list<values> _output;
	
	processor(int n)
	: generik::threadgenerator<std::string, values>(n)
	{
		GENERIK_TRACER;
	}
	
	bool empty()
	{
		return _input.empty();
	}
	std::string pop()
	{
		std::string in = _input.front();
		_input.pop_front();
		return in;
	}
	void push(values& out)
	{
		_output.push_back(out);
	}
	
	values process(const std::string& in)
	{
		GENERIK_TRACER;
		values result(in);
		return result;
	}
};

int main(int argc, char** argv)
{
	GENERIK_TRACER;
	
	options opts(argc, argv);
	
	std::ifstream file("test.csv");
	if (!file.is_open())
	{
		GENERIK_ERROR("could not open test.csv for reading");
		exit(-1);
	}
	
	if (argc < 1)
	{
		GENERIK_ERROR("need at least one argument (number of threads to generate)");
		exit(-2);
	}
	
	processor _processor(opts.nthreads);
	
	_processor._read.lock();
	std::string line;
	std::getline(file, line);
	while (!file.eof())
	{
		_processor._input.push_back(line);
		std::getline(file, line);
	}
	std::cout<<"read "<<_processor._input.size()<<" lines"<<std::endl;
	_processor._read.unlock();
	
	_processor.execute();
	std::cout<<"processing..."<<std::endl;
	_processor.finish();
	
	GENERIK_TRACE_VALUE(_processor._input.size());
	GENERIK_TRACE_VALUE(_processor._output.size());
	
	std::cout<<"processed "<<_processor._output.size()<<" records"<<std::endl;
	
	GENERIK_FOR_EACH(i, _processor._output)
	{
		//std::cout<<i->_values.size()<<std::endl;
	}
	
	return 0;
}



